package com.health.mapp.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;

import com.health.commons.PropertiesService;
import com.health.db.dto.StuffDetailDto;
import com.health.db.model.ShareInfo;
import com.health.db.model.StuffListTemp;
import com.health.db.model.UserAccountLedger;
import com.health.db.model.UserCardList;
import com.health.db.model.UserCardListExt;
import com.health.db.model.UserLIST;
import com.health.db.model.UserLISTTemp;
import com.health.db.service.user.IStuffListService;
import com.health.db.service.user.IUserAccountLedgerService;
import com.health.db.service.user.IUserCardListService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryUserReq;
import com.health.mapp.bean.QueryUserResp;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;

/**
 * 个人钱包 
 * 注释：余额显示为 钱包余额+卡余额
 * @author HiRoad
 *
 */
public class QueryUserImpl  implements ActionInterface {

	private static Logger logger = Logger.getLogger(QueryUserImpl.class);
	private IUserLISTService userLISTService;
	private IUserCardListService userCardListService;
	private IStuffListService stuffListService  ;
	private IUserAccountLedgerService userAccountLedgerService ;
	
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		QueryUserReq req=(QueryUserReq) reqBean;
		QueryUserResp resp = new QueryUserResp();
	    
	    long userId=Long.valueOf(req.getUserId());
	    
	    UserLIST userLIST= userLISTService.selectByUserId(userId);
	    
	    UserLISTTemp userLISTTemp=new UserLISTTemp();
		userLISTTemp.setUserId( String.valueOf(userLIST.getUserid()));
		userLISTTemp.setUserName(userLIST.getUserName());
		userLISTTemp.setUserPhone(userLIST.getUserNo());
		userLISTTemp.setRegTime(userLIST.getRegisterDate());
		userLISTTemp.setIcon(userLIST.getIconUrl());
		userLISTTemp.setBusinessCode(userLIST.getBusinessCode()==null?"-":userLIST.getBusinessCode());
		
		//查询用户卡
		UserCardListExt example =new UserCardListExt();
		example.setOwnerid(userId);
		List<UserCardList>  list =userCardListService.selectUserCardLists(example);
		int leftTime01 =0;
		BigDecimal leftTime02 = new BigDecimal("0");
		int leftTime03 =0;
		int leftTime04 =0;
		int leftTime05 =0;
		
		//查询是否为vip
		if (null!=userLIST.getVipLevel()&&null!=userLIST.getVipEndDate()) {
			
			if (userLIST.getVipEndDate().compareTo(DateUtils.now())>0) {
				userLISTTemp.setVipLevel(userLIST.getVipLevel().toString());
			}
		}
		
		for (int i = 0; i < list.size(); i++) {
			UserCardList userCardList = (UserCardList) list.get(i);
			if (null!=userCardList.getLefttime01())leftTime01 = leftTime01+userCardList.getLefttime01().intValue();
			if (null!=userCardList.getLefttime02())leftTime02 = leftTime02.add(userCardList.getLefttime02());
			if (null!=userCardList.getLefttime03())leftTime03 = leftTime03+userCardList.getLefttime03().intValue();
			if (null!=userCardList.getLefttime04())leftTime04 = leftTime04+userCardList.getLefttime04().intValue();
			if (null!=userCardList.getLefttime05())leftTime05 = leftTime04+userCardList.getLefttime05().intValue();
		}
		
		/*UserAccountLedger  userAccountLedger = userAccountLedgerService.selectByUserId(userId);
		
		if (null!=userAccountLedger&&null!=userAccountLedger.getBalanceTotal()) {
			userLISTTemp.setBalance(String.valueOf(leftTime02.add(userAccountLedger.getBalanceTotal()).setScale(2,  BigDecimal.ROUND_HALF_UP)));
		}else{
			userLISTTemp.setBalance(String.valueOf(leftTime02.setScale(2,  BigDecimal.ROUND_HALF_UP)));
		}*/
		userLISTTemp.setBalance(String.valueOf(leftTime02.setScale(2,  BigDecimal.ROUND_HALF_UP)));
		userLISTTemp.setLeftTime01(leftTime01);
		
		//userLISTTemp.setLeftTime02(String.valueOf(leftTime02.setScale(2,  BigDecimal.ROUND_HALF_UP)));
		userLISTTemp.setLeftTime03(leftTime03);
		userLISTTemp.setLeftTime04(leftTime04);
		userLISTTemp.setLeftTime05(leftTime05);
		
		if (userLIST.getTypeid().intValue()==3) {

			StuffDetailDto stuffDetailDto = stuffListService.selectByStuffId(userLIST.getUserid());

			StuffListTemp detailDto = new StuffListTemp();
	        detailDto.setDocId(String.valueOf(stuffDetailDto.getStuffid()));
	        detailDto.setDocName(stuffDetailDto.getStuffName());
	        detailDto.setDocTitle(stuffDetailDto.getStuffTitle());
	        detailDto.setDocLable(stuffDetailDto.getStuffLabel());
	        detailDto.setDocImageUrl((stuffDetailDto.getIconUrl())==null?PropertiesService.getString("iconUrl.docDefaultUrl").trim():stuffDetailDto.getIconUrl());
	        //医生头像以doc中的头像为主
	    	userLISTTemp.setIcon(detailDto.getDocImageUrl());
	        detailDto.setHospitalId(stuffDetailDto.getEntityid()==null?2:stuffDetailDto.getEntityid());
	        detailDto.setHospitalName(stuffDetailDto.getHospitalName()==null?"":stuffDetailDto.getHospitalName());
	        detailDto.setDepartmentId(stuffDetailDto.getDepartmentid()==null?0:stuffDetailDto.getDepartmentid());
	        detailDto.setDepartmentName(stuffDetailDto.getDepartmentName()==null?"":stuffDetailDto.getDepartmentName());
	  
	        UserAccountLedger  userAccountLedger = userAccountLedgerService.selectByUserId(userId);
	        
	        if (null!=userAccountLedger&&null!=userAccountLedger.getBalanceTotal()) {
				userLISTTemp.setBalance(String.valueOf(userAccountLedger.getBalanceTotal().setScale(2,  BigDecimal.ROUND_HALF_UP)));
			}
	        detailDto.setDocShareUrl("http://shouer-share.shouerhealth.cn/docShare/2-"+String.valueOf(stuffDetailDto.getStuffid())+".html");
	        
	        detailDto.setDocShareTip("扫一扫二维码图案，下载首尔健康汇#收藏专家");
	        
	        resp.setDoctor(detailDto);
		}
		resp.setUser(userLISTTemp);

		//测试
		ShareInfo shareInfo =new ShareInfo();
		
		shareInfo.setUrl("docShare/2-494503.html");
		shareInfo.setTitle("儿科医生吴金屏 与你共同呵护儿童健康");
		shareInfo.setSubTitle("69年入伍，73年就读白求恩医科大学医疗系，79年转业北京市第二医院儿科");
		shareInfo.setIconUrl("image/icon.png");
		resp.setShareInfo(shareInfo);
		
		return resp;
	}
	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}
	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}
	/**
	 * @return the userCardListService
	 */
	public IUserCardListService getUserCardListService() {
		return userCardListService;
	}
	/**
	 * @param userCardListService the userCardListService to set
	 */
	public void setUserCardListService(IUserCardListService userCardListService) {
		this.userCardListService = userCardListService;
	}
	/**
	 * @return the stuffListService
	 */
	public IStuffListService getStuffListService() {
		return stuffListService;
	}
	/**
	 * @param stuffListService the stuffListService to set
	 */
	public void setStuffListService(IStuffListService stuffListService) {
		this.stuffListService = stuffListService;
	}
	/**
	 * @return the userAccountLedgerService
	 */
	public IUserAccountLedgerService getUserAccountLedgerService() {
		return userAccountLedgerService;
	}
	/**
	 * @param userAccountLedgerService the userAccountLedgerService to set
	 */
	public void setUserAccountLedgerService(
			IUserAccountLedgerService userAccountLedgerService) {
		this.userAccountLedgerService = userAccountLedgerService;
	}

	
	
}
